package org.fastiot

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class MetaFieldSpec extends Specification implements DomainUnitTest<MetaField> {

    def setup() {
    }

    def cleanup() {
    }

    void "Test a valid properties"() {
        given:
        def instance = new MetaField(
                bind: bind, bindCodes: bindCodes, code: code, label: label,
                subcategory: subcategory, layoutIndex: layoutIndex,
                template: new MetaField.Template(
                        inputForm: inputForm, sourceType: sourceType,
                        sourceDefinition: sourceDefinition, nullable: nullable
                ), enabled: enabled
        )

        when:
        instance.save(flush: true)

        then:
        !instance.hasErrors()

        MetaField.where {
            bind == bind && (bindCodes == null || bindCodes ==~ bindCodes)
        }.count() == 1

        where:
        bind << [MetaField.BindDomain.Terminal, MetaField.BindDomain.Model, MetaField.BindDomain.Group]
        bindCodes << [null, "model1,model2", null]
        code << ["hardwareVer", "softwareVer", "productSN"]
        label << ["硬件版本号", "软件版本号", "生产编号"]
        subcategory << [null, "byModel", "byGroup"]
        layoutIndex << [1, 2, 3]
        inputForm << [MetaField.Template.InputForm.Text, MetaField.Template.InputForm.Select, MetaField.Template.InputForm.Select]
        sourceType << [null, MetaField.Template.SelectSource.LocalJSON, MetaField.Template.SelectSource.NetworkJSON]
        sourceDefinition << [null, "[{'code':'abc','value':1},{'code':'def','value':2}]", "http://example.fastiot.org/template/sourceDefinition.json"]
        nullable << [true, false, true]
        enabled << [true, false, false]
    }

    void "Test an invalid properties"() {
        given:
        def instance = new MetaField(
                bind: bind, bindCodes: bindCodes, code: code, label: label,
                subcategory: subcategory, layoutIndex: layoutIndex,
                template: new MetaField.Template(
                        inputForm: inputForm, sourceType: sourceType,
                        sourceDefinition: sourceDefinition, nullable: nullable
                ), enabled: enabled
        )

        when:
        instance.save(flush: true)

        then:
        instance.hasErrors()
        invalidFields.collect { field ->
            instance.errors.getFieldErrorCount(field)
        }.sum() == instance.errors.errorCount

        where:
        bind << [null]
        bindCodes << [null]
        code << ["hardwareVer"]
        label << ["硬件版本号"]
        subcategory << [null]
        layoutIndex << [-1]
        inputForm << [MetaField.Template.InputForm.Text]
        sourceType << [MetaField.Template.SelectSource.LocalJSON]
        sourceDefinition << ["http://example.fastiot.org/template/sourceDefinition.json"]
        nullable << [true]
        enabled << [true]
        invalidFields << [['bind', 'inputForm', 'layoutIndex']]
    }
}
